﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using JDEduTrain.Framework.Business;
using JDEduTrain.Framework.ViewEntity;
using JDEduTrain.Framework.Entity;
using Wicresoft.Framework.Organization;

public partial class Page_FinishClassList : BaseAuthenticationPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 班级名称";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "searchClick();");
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.ShowAdvancedSearch = false;

        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "CreateTime";
            this.Order.OrderDirection = "DESC";
            BindClass();
        }
    }

    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void BindClass()
    {
        int recordCount = 0;
        //暂时处理为 终审通过的班级 均可完结
        if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.MinistryAdminRoleID))
        {
            Department currMainDept = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
            string deptPath = currMainDept.IDPath;
            string[] deptIDs = deptPath.Split('/');
            string mainDeptID = string.Empty;
            if (deptIDs.Length >= 4)
            {
                mainDeptID = deptIDs[3];
            }
            DataTable dtAuditClass = BLTrain.GetAuditedClassList(0, int.MaxValue, SearchClause, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
            DTUserPageList = dtAuditClass.Clone();
            DTUserPageList.Clear();
            recordCount = 0;
            //筛选该单位人事负责的班级
            foreach (DataRow dr in dtAuditClass.Rows)
            {
                try
                {
                    dr["BeginTime"] = Convert.ToDateTime(dr["BeginTime"]).ToShortDateString();
                    dr["EndTime"] = Convert.ToDateTime(dr["EndTime"]).ToShortDateString();
                }
                catch (Exception)
                {
                    dr["BeginTime"] = DateTime.MinValue;
                    dr["EndTime"] = DateTime.MinValue;
                }
                if (!string.IsNullOrEmpty(mainDeptID))
                {
                    if (OrganizationHelper.IsUserInDepartment(new Guid(dr["CreateUserID"].ToString()), new Guid(mainDeptID)))
                    {
                        DTUserPageList.ImportRow(dr);
                        recordCount++;
                    }
                }
            }
            DTUserPageList = WebCommon.GetPagedTable(DTUserPageList, Pager.PageIndex + 1, Pager.PageSize);

        }
        else
        {
            //不需要初审
           // SearchClause += string.Format("AND ( {0} = '{1}' OR {0} = '{2}') ", TrainClass.SQLCOL_FINISHSTATUS, "初审结束","已结束");
            DTUserPageList = BLTrain.GetAuditedClassList(Pager.PageIndex, Pager.PageSize, SearchClause, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
        }
        Pager.RecordCount = recordCount;
        foreach (DataRow dr in DTUserPageList.Rows)
        {
            if (string.IsNullOrEmpty(dr["FinishStatus"].ToString()))
            {
                dr["FinishStatus"] = "未结束";
            }
        }
        rptUserInfo.DataSource = DTUserPageList;
        rptUserInfo.DataBind();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindClass();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause += string.Format("AND {0} LIKE N'%{1}%'", TrainClass.SQLCOL_CLASSNAME, tbSearch.Text);
        else
            SearchClause = "1=1";
        BindClass();

    }
    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        BindClass();
    }
    protected void btnFinish_Click(object sender, EventArgs e)
    {
        ArrayList alClass = new ArrayList();

        for (int i = 0; i < rptUserInfo.Items.Count; i++)
        {
            if ((rptUserInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptUserInfo.Items[i].ItemIndex]["ID"].ToString());
                alClass.Add(id);
            }
        }
        string script = "alert('操作成功！')";
        if (alClass.Count > 0)
        {
            BLTrain.FinishClass((Guid[])alClass.ToArray(typeof(Guid)), OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID));
            BindClass();
        }
        else
        {
            script = "alert('请选择班级！')";
        }
        Page.ClientScript.RegisterStartupScript(GetType(), "modify", script, true);
    }

    protected void rptUserInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlTableCell tc = e.Item.FindControl("tdAdminCB") as HtmlTableCell;
            //if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID))
            //{
            //    tc.Visible = false;
            //}
        }
    }
}
